package com.easy.dev.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.dev.bo.ResponseStatus;
import com.easy.dev.dto.PageDTO;
import com.easy.dev.service.impl.BaseServiceImpl;
import com.easy.dev.utils.WrapperUtils;
import com.easy.dev.vo.Result;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.Map;

public abstract class BaseController<T> {

    protected Class<?> entityClass = currentModelClass();

    protected Class currentModelClass() {
        return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    abstract BaseServiceImpl getService();

    @PostMapping("/save")
    public Result save(T obj) {
        int i = getService().insert(obj);
        return getResult(ResponseStatus.SAVE_SUCCESS);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(Serializable id) {
        int i = getService().deleteById(id);
        return getResult(ResponseStatus.DELETE_SUCCESS);
    }

    @DeleteMapping("/{ids}")
    public Result deleteBatchIds(@PathVariable("ids")Collection<? extends Serializable> idList) {
        int i = getService().deleteBatchIds(idList);
        return getResult(ResponseStatus.DELETE_SUCCESS);
    }

    @PutMapping("/update")
    public Result update(T entity) {
        int i = getService().updateById(entity);
        return getResult(ResponseStatus.UPDATE_SUCCESS);
    }

    @PostMapping("/page")
    public Result selectPage(@RequestBody PageDTO dto) {
        Page<T> page = new Page<>(dto.getCurrentPage(),dto.getPageSize());
        Page<T> data = (Page<T>) getService().selectPage(page,null);
        return getResult(ResponseStatus.UPDATE_SUCCESS, data);
    }

    @PostMapping("/page/condition")
    public Result selectMapsPage(@RequestBody Map<String, Object> params) {
        verificationParam(params);
        PageDTO dto = getPageDTOByParams(params);
        removePageDTOInParams(params);
        Page<T> page = new Page<>(dto.getCurrentPage(),dto.getPageSize());
        Page<T> data = (Page<T>) getService().selectPage(page, WrapperUtils.getWrapperByParams(params, entityClass));
        return getResult(200,"",data);
    }

    Result getResult() {
        return new Result();
    }

    Result getResult(int i) {
        return new Result();
    }

    Result getResult(ResponseStatus responseStatus) {
        return new Result(responseStatus);
    }

    Result getResult(ResponseStatus responseStatus, Object data) {
        return new Result(responseStatus, data);
    }

    Result getResult(int i, String msg, Object data) {
        return new Result(i,msg,data);
    }

    private void verificationParam(Map<String, Object> params) {
        // TODO  校验参数
    }

    private PageDTO getPageDTOByParams(Map<String, Object> params) {
        int currentPage = (Integer)params.get("currentPage");
        int pageSize = (Integer) params.get("pageSize");
        PageDTO dto = new PageDTO();
        dto.setCurrentPage(currentPage);
        dto.setPageSize(pageSize);
        return dto;
    }

    private void removePageDTOInParams(Map<String, Object> params) {
        params.remove("currentPage");
        params.remove("pageSize");
    }

}
